#ifndef _PORT_H_
#define _PORT_H_

#include "os.h"


typedef int (*port_cb_accept)(int portfd, int sockfd);
typedef int (*port_cb_close)(int portfd, int linefd);
typedef int (*port_cb_packet)(int portfd, int linefd, int msgid, int pktfd);


typedef struct LoadLine
{
    struct sockaddr_in addr;
    int linefd;
}LoadLine;

typedef struct Port
{
    int used;
    int listenfd;
    int portfd;
    int count_connection;
    
    void *userdata;
    
    port_cb_accept cb_accept;//一个client连接上来了
    port_cb_packet cb_packet;//一个完整的包到达了
    port_cb_close cb_close;//一个连接关闭了

    int lua_cb_self;
    char lua_cb_packet[128];
    //负载均衡
    LoadLine *load_lines;
    int load_line_num;
    //ps. 要放在后面
    struct event evt;     
        
}Port;

typedef struct Line
{
    int used;
    int portfd;
    int sockfd;
    
    //读缓冲区
    int buf_read;
    //读缓冲区
    int buf_write;
    
    //ps. 要放在后面
    struct event ev_read;
    struct event ev_write;
}Line;

int line_get_write_buffd(int fd);
int line_is_close(int fd);

int port_init();

int port_new();

int port_listen(int portfd, unsigned short portno);
int port_connect(int portfd, const char *ip, unsigned short portno);
int port_connect_addr(int portfd, struct sockaddr_in addr);

int port_accept(int portfd, int sockfd);
int port_reject(int portfd, int sockfd);

void  port_set_userdata(int portfd, void *userdata);
void *port_get_userdata(int portfd);

int port_set_lua_cb_packet(int portfd, int self, const char *funcname);
int port_set_cb_accept(int portfd, port_cb_accept cb);
int port_set_cb_packet(int portfd, port_cb_packet cb);


int port_close_line(int linefd);


int port_close(int portfd);
int port_select(int portfd, int base);

#endif
